<?php
/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2018/5/22
 * Time: 下午5:19
 */

namespace common\service;

use common\models\CouponCategory;
use common\models\UserCoupon;
use common\models\User;
use common\models\Coupon;
use yii;
use yii\mongodb\Database;

/**
 * @SuppressWarnings(PHPMD)
 */
class CouponService
{


    public function getCouponByUserid($userid){

        $database = Yii::$app->mongo->getDatabase();
        $collection = $database->getCollection("coupon");
        $coupon = $collection->findOne(['user_id' => intval($userid)]);
        return $coupon;
    }

    public function getCouponByPromotion(){
        $userid=Yii::$app->user->id;

        try {
            $user =  User::findOne($userid);
            if (!empty($user)) {

                if($user->status==1){
                    return ['code' => -2, 'message' => "不好意思，您无法领取该优惠券~"];
                }
                return $this->Promotion($user->id);
            }

            return ['code' => -1, 'message' => "重新登陆"];
        } catch (\Exception $e) {
            return ['code' => -99, 'message' => $e->getMessage()];
        }
    }
    function Promotion($userid){
        //是否领过，如果领过报领过提示
        $couponCategoryList = CouponCategory::getCollection()->find(['like','local_mark_id','cuxiao'],'mark_id')->toArray();
        $tmp=[];
        foreach($couponCategoryList as $couponCategory){

            $tmp[]=$couponCategory['mark_id'];
            $coupon = Coupon::getCollection()->findOne(['catalog_mark_id'=>$couponCategory['mark_id']]);

            $startTime = $coupon['created_at'];
            $endTime = $coupon['expire_at'];
             if(time()<$startTime || time()>$endTime){

                return ['code' => -4, 'message' => '不在活动期间'];
            }
        }
        $coupons = UserCoupon::getCollection()->findOne(['user_id'=>$userid,'mark_id'=>$tmp]);


            if(!empty($coupons)){
                $coupon = Coupon::getCollection()->findOne(['catalog_mark_id'=>$coupons['mark_id']]);
                return ['code' => -3, 'message' => '您已经领过了','data'=>$coupon];
            }

//        //查看当前领券人数
//        $users=UserCoupon::getCollection()->find(['mark_id'=>$tmp])->toArray();
//        $usersTotal=count($users);


            $result= $this->getCouponByLocalMarkId('cuxiao_1',$userid);
           if($result['code']==0 || ($result['code']==-99 && strpos($result['message'],"领完了")>0) ){
               $result= $this->getCouponByLocalMarkId('cuxiao_2',$userid);
               if($result['code']==0 || ($result['code']==-99 && strpos($result['message'],"领完了")>0)){
                   $result= $this->getCouponByLocalMarkId('cuxiao_3',$userid);
                   if($result['code']==0 || ($result['code']==-99 && strpos($result['message'],"领完了")>0)){
                       return ['code' => 0, 'message' => '没有优惠券或活动已结束'];
                   }
               }

           }

      
        return $result;


    }
    function getCouponByLocalMarkId($category,$userid){

        $couponCategorys = CouponCategory::getCollection()->find(['local_mark_id' =>$category, 'status' => ''.CouponCategory::STATUS_ON])->toArray();

        if (count($couponCategorys)==0) {
            return ['code' => 0, 'message' => '没有优惠券或活动已结束1'];
        }

        $couponCategory=$couponCategorys[0];

        try{
            $coupon = UserService::createUserCoupon($couponCategory, $userid);
            return ['code' =>1, 'message' => '领取成功','data'=>[$coupon]];
        } catch (\Exception $e) {
            return ['code' => -99, 'message' => $e->getMessage()];
        }


    }



}
